#! /usr/bin/env python

# Compute the S/N from the last N minutes of an observation

import psrchive

from optparse import OptionParser
usage = """Usage: %prog [options] file (file2 ...)

Loads a data file and sums up the profiles in the last N minutes of the
observation, then returns the S/N """


par = OptionParser(usage=usage)
par.add_option("-m", "--min", dest="minutes", 
               help="Number of minutes to integrate",
               action="store", type="float", default="5.0")
(opt,args) = par.parse_args()

for f in args:

    ar = psrchive.Archive_load(f)
    nsubint = ar.get_nsubint()
    
    total = ar.get_Integration(nsubint-1).total()

    for isub in range (nsubint-2, -1, -1):
        subtot = ar.get_Integration(isub).total()
        total.combine(subtot)
        
        if (total.get_duration() > 600):
            break

    profile = total.get_Profile(0,0);
    print "S/N=", profile.snr();
